package cn.iocoder.yudao.module.member.controller.admin.studentCurriculumVitae;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.member.controller.admin.student.vo.StudentRespVO;
import cn.iocoder.yudao.module.member.controller.admin.studentCurriculumVitae.vo.*;
import cn.iocoder.yudao.module.member.convert.studentCurriculumVitae.StudentCurriculumVitaeConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.student.StudentDO;
import cn.iocoder.yudao.module.member.dal.dataobject.studentCurriculumVitae.StudentCurriculumVitaeDO;
import cn.iocoder.yudao.module.member.service.student.StudentService;
import cn.iocoder.yudao.module.member.service.studentCurriculumVitae.StudentCurriculumVitaeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 -运营端- 用户简历")
@RestController
@RequestMapping("/member/student-curriculum-vitae")
@Validated
@Slf4j
public class StudentCurriculumVitaeController {

    @Resource
    private StudentCurriculumVitaeService studentCurriculumVitaeService;
    @Resource
    private StudentService studentService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private static final String REDIS_VITAE="MEMBER_CURRICULUM_VITAE:";

    public boolean getExistRedis(Long studentId){
        synchronized(this){
            String sKey=REDIS_VITAE+studentId;
            if(stringRedisTemplate.hasKey(sKey)){
                return true;
            }else{
                stringRedisTemplate.opsForValue().set(sKey, "1", 3, TimeUnit.SECONDS);
                return false;
            }
        }
    }
    @PostMapping("/create")
    @ApiOperation("创建用户简历")
    public CommonResult<Long> createStudentCurriculumVitae(@Valid @RequestBody StudentCurriculumVitaeCreateReqVO createReqVO) {
        if(getExistRedis(createReqVO.getStudentId())){
            log.info("调用创建简历重复,运营端调用,参数:"+ SecurityFrameworkUtils.getLoginUserId()+ JsonUtils.toJsonString(createReqVO));
            return error("操作繁忙,请稍后重试");
        }else{
            log.info("创建简历,运营端调用,参数:"+createReqVO.getStudentId());
        }
        return success(studentCurriculumVitaeService.createStudentCurriculumVitae(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户简历")
    @TenantIgnore
    public CommonResult<Boolean> updateStudentCurriculumVitae(@Valid @RequestBody StudentCurriculumVitaeUpdateReqVO updateReqVO) {
        studentCurriculumVitaeService.updateStudentCurriculumVitae(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户简历")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deleteStudentCurriculumVitae(@RequestParam("id") Long id) {
        studentCurriculumVitaeService.deleteStudentCurriculumVitae(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户简历")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<StudentCurriculumVitaeRespVO> getStudentCurriculumVitae(@RequestParam("id") Long id) {
        StudentCurriculumVitaeDO studentCurriculumVitae = studentCurriculumVitaeService.getStudentCurriculumVitae(id);
        return success(StudentCurriculumVitaeConvert.INSTANCE.convert(studentCurriculumVitae));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户简历列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<StudentCurriculumVitaeRespVO>> getStudentCurriculumVitaeList(@RequestParam("ids") Collection<Long> ids) {
        List<StudentCurriculumVitaeDO> list = studentCurriculumVitaeService.getStudentCurriculumVitaeList(ids);
        return success(StudentCurriculumVitaeConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/page")
    @ApiOperation("获得用户简历分页")
    public CommonResult<PageResult<StudentCurriculumVitaeRespVO>> getStudentCurriculumVitaePage(@RequestBody StudentCurriculumVitaePageReqVO pageVO) {
        PageResult<StudentCurriculumVitaeDO> pageResult = studentCurriculumVitaeService.getStudentCurriculumVitaePage(pageVO);
        return success(StudentCurriculumVitaeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户简历 Excel")
    @OperateLog(type = EXPORT)
    public void exportStudentCurriculumVitaeExcel(@Valid StudentCurriculumVitaeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        if(null==exportReqVO.getPageSize()){
            exportReqVO.setPageNo(1);
            exportReqVO.setPageSize(10);
        }
        List<StudentCurriculumVitaeExcelVO> datas = studentCurriculumVitaeService.exportStudentCurriculumVitaeExcel(exportReqVO);
        ExcelUtils.write(response, "用户简历.xls", "数据", StudentCurriculumVitaeExcelVO.class, datas);
    }


    @GetMapping("/getByStudentId")
    @ApiOperation("通过学生ID获得用户简历")
    @TenantIgnore
    @ApiImplicitParam(name = "studentId", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<StudentRespVO> getStudentCurriculumVitaeByStudentId(@RequestParam("studentId") Long studentId) {
        StudentDO studentDO=studentService.getStudent(studentId);
        StudentRespVO studentRespVO=new StudentRespVO();
        BeanUtils.copyProperties(studentDO,studentRespVO);
        List<StudentCurriculumVitaeDO> studentCurriculumVitae = studentCurriculumVitaeService.getStudentCurriculumVitaeByStudentId(studentId);
        studentRespVO.setStudentCurriculumVitaeRespVOList(StudentCurriculumVitaeConvert.INSTANCE.convertList(studentCurriculumVitae));
        return success(studentRespVO);
    }

}
